import { ref ,reactive, onBeforeMount,type UnwrapRef} from 'vue' 
 import type {  Entity ,Department} from "@/api/types";
 import http from "@/utils/request";
 import { ElMessage } from 'element-plus'

export default  function<T extends Entity>(url:string,getByIdURL:string,clostDialog:()=>{},id:number,initObj:T){

     const form = ref<T>(initObj)
     
    //定义方法
    function onSubmit(){
       
    
        console.log("form.value ",form.value)
        http.post<boolean>(url,form.value).then(data=>{
            if (data) {
                ElMessage({ message: '保存成功', type: 'success', })
                clostDialog()
            } else {
             ElMessage({ message: '删除失败了', type: 'error', })
            }
        })
    }
    
    
    onBeforeMount(()=>{
      
      if(id){
        //根据id查询待修改的表单
        const url = getByIdURL+id
        http.get<UnwrapRef<T>>(url).then(department=>{
            form.value = department;
        })
      }
    })
    


    return {
        form,
        onSubmit
    }

}